//
//  RecreationManager.m
//  Happy to chat
//
//  Created by lanou3g on 15/12/8.
//  Copyright © 2015年 LL. All rights reserved.
//

#import "RecreationManager.h"
#import "AFHTTPRequestOperationManager.h"
#import "RecreationModel.h"
@import AVFoundation;
@interface RecreationManager ()
//声明一个模型数组
@property (nonatomic, strong)NSMutableArray *modelArray;
//创建播放器
@property (nonatomic, strong)AVPlayerItem *avplayerItem;
@property (nonatomic, assign)NSInteger AVIndex;
@property (nonatomic, strong)NSMutableArray *detaiModelArray;
@property (nonatomic, assign)BOOL detailORNO;

@end


static RecreationManager *recreationManager = nil;
@implementation RecreationManager
//初始化单利
+(instancetype)defaultRecraetionManager
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        recreationManager = [RecreationManager new];
    });
    return recreationManager;
}
/**
 *  重写初始化方法
 */
-(instancetype)init{
    
    if (self = [super init]) {
        self.AVIndex = 0;
        self.UPorDown = YES;
        self.detailORNO = YES;
    }
    return self;
}
/**
 modelArray懒加载
 */
- (NSMutableArray *)modelArray {
    if (_modelArray == nil) {
        _modelArray = [NSMutableArray array];
    }
    return _modelArray;
}



/**
 *  赖加载
 */
-(NSMutableArray *)detaiModelArray{
    if (_detaiModelArray == nil) {
        _detaiModelArray = [NSMutableArray array];
    }
    return _detaiModelArray;
    
}
/**
 加载页面准备
 */
- (void)prepareMoviePageWithHandle:(void(^)())handle  {
    self.detailORNO = NO;
    if (self.UPorDown == YES) {
        NSString *string = [NSString stringWithFormat:@"http://c.3g.163.com/nc/video/home/%ld-10.html",self.AVIndex];
        [self requestWithURLStr:string handel:handle key:@"videoList"];
        self.AVIndex = self.AVIndex + 10;
    }else if (self.UPorDown == NO){
        NSString *string = @"http://c.3g.163.com/nc/video/home/0-10.html";
        [self requestWithURLStr:string handel:handle key:@"videoList"];
    }
}

/**
 视频详情准备加载
 */
- (void)prepareDetailMovieWithHandel:(void(^)())handle
                           URLString:(NSString *)urlStr
                                 key:(NSString *)key {
    self.detailORNO = YES;
    [self.detaiModelArray removeAllObjects];
    [self requestWithURLStr:urlStr handel:handle key:key];
}

/**
 解析数据
 */
- (void)requestWithURLStr:(NSString *)urlStr
                   handel:(void(^)())handle
                      key:(NSString *)key{
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    //NSLog(@"%@",manager);
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
    [manager GET:urlStr parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        NSDictionary *sourceDict = [NSDictionary dictionaryWithDictionary:responseObject];
        NSArray *videoArray = sourceDict[key];
        for (NSDictionary *dic in videoArray) {
            RecreationModel *movie = [[RecreationModel alloc]init];
            [movie setValuesForKeysWithDictionary:dic];
            //NSLog(@"%@",dic);
            if (self.detailORNO == NO) {
                [self.modelArray addObject:movie];
            }else if (self.detailORNO == YES){
                [self.detaiModelArray addObject:movie];
            }
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            handle();
        });
    } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
        NSLog(@"解析失败");
        NSLog(@"%@",error);
    }];
}


/**
 加载播放器
 */
- (void)setupAVPlayerWithModel:(RecreationModel *)movie {
    NSURL *url=[NSURL URLWithString:movie.mp4_url];
    self.avplayerItem = [AVPlayerItem playerItemWithURL:url];
    self.avPlayer = [AVPlayer playerWithPlayerItem:self.avplayerItem];
    self.avPlayerlayer = [AVPlayerLayer  playerLayerWithPlayer:self.avPlayer];
}
/**
 开始播放
 */
- (void)playerPlay
{
    [self.avPlayer play];
}

/**
 暂停播放
 */
- (void)playerPause
{
    [self.avPlayer pause];
}


/**
 返回model数组
 */
- (NSArray *)returnModelArray {
    return self.modelArray;
}

/**
 返回detailModelArray
 */
- (NSArray *)returnDetailModelArray {
    return self.detaiModelArray;
}

/**
 返回数组个数
 */
- (NSInteger)returnModelArrayCount {
    return self.modelArray.count;
}

/**
 返回model
 */
- (RecreationModel *)returnMovieModelWithIndex:(NSInteger)index {
    return self.modelArray[index];
}

/**
 释放avPlayer
 */
- (void)shiFang {
    [self.avPlayerlayer removeFromSuperlayer];
    self.avPlayerlayer = nil;
    self.avPlayer = nil;
    self.avplayerItem = nil;
}




@end
